const mongoose = require('mongoose');
const Cate = require('../models/cate.model');

//新增
exports.create = function(req, res, next) {
  // const cate = new Cate(req.body);
  // cate.save().then((data) => {
  //     res.json(data);
  // });
  if (req.body.parentId != null) {
    req.params.parentId = req.body.parentid;
    var product = req.params.product ? req.params.product : null;
    var parentId = req.params.parentId ? req.params.parentId : null;
    const cate = new Cate(req.body);
    cate.save().then(data => {
      res.json(data);
    });
  } else {
    var product = req.params.product ? req.params.product : null;
    const cate = new Cate(req.body);
    cate.save().then(data => {
      res.json(data);
    });
  }
};
//更新
exports.update = function(req, res, next) {
  var id = req.params.id;
  Cate.findByIdAndUpdate(id, { $set: req.body }, { new: false }).then(data => {
    res.json(data); //这里是修改前的数据，不是修改后的数据
  });
};
//删除
exports.remove = function(req, res, next) {
  const id = req.params.id;
  // Cate.findByIdAndDelete(id,(err,data)=>{
  //     res.json(data);
  // })
  // console.log(id);
  // del(id);
  var ids = [];
  Cate.findOne({ _id: id }, function(err, doc) {
    ids = [doc._id];
    doc.getChildren(function(err, docs) {
      for (var i = 0; i < docs.length; i++) {
        ids.push(docs[i]._id);
      }

      Cate.remove({ _id: { $in: ids } }).then(data => {
        res.json({ message: '删除成功', status: 200 });
      });
    });
  });
};

// function del(id){
//     Cate.find({_id:id}).then(data1=>{
//         if(data1){
//         	if(!data1[0]._id){data1[0]._id=null}
//             Cate.findByIdAndDelete(data1[0]._id,(err,data)=>{console.log(err)});
//             Cate.find({parentId:id}).then(data2=>{
//                 if(data2){
//                     for(var i=0;i<data2.length;i++){
//                         del(data2[i]._id);
//                         Cate.findByIdAndDelete(data2[i]._id,(err,data)=>{});
//                     }
//                 }
//             })
//         }
//     })
// }

//查询
function reverseTree(data, pid) {
  var result = [],
    temp;
  var data = JSON.parse(JSON.stringify(data)); //将数据模型的对象转成字符串，再将字符串转成Js.object
  //用json是因为模型内是没有children这个属性节点的，但后期又要加入children属性，所以这种转换后期就可以随意加
  for (var i in data) {
    if (data[i].parentId == pid) {
      result.push(data[i]);
      temp = reverseTree(data, data[i]._id);
      if (temp.length > 0) {
        data[i].children = temp;
      }
    }
  }
  return result;
}

exports.list = function(req, res, next) {
  var type = req.params.type || 1;
  Cate.find({ type: type }).then(data => {
    // console.log(data); //返回一个数组  说明可以进行递归操作

    var rpTree = reverseTree(data, null);
    res.json(rpTree);
    // console.log(rpTree[0].children[0]);
  });
};
//通过id
exports.getData = function(req, res, next) {
  var id = req.params.id;
  Cate.findById(id).then(data => {
    res.json(data);
  });
};

exports.c = function(req, res, next) {
  var id = req.params.id;
  del(id);
};
